package com.ls.util;

import com.ls.entity.User;
import org.springframework.stereotype.Component;

/**
 * 持有用户信息，用于代替session对象
 *
 * @author ls
 */
@Component
public class UserContextHolder {

    /**
     * 线程本地变量
     */
    private static final ThreadLocal<User> USER_CONTEXT_HOLDER = new ThreadLocal<>();

    public void set(User user) {
        USER_CONTEXT_HOLDER.set(user);
    }

    public User getUser() {
        return USER_CONTEXT_HOLDER.get();
    }

    public void clear() {
        USER_CONTEXT_HOLDER.remove();
    }
}
